#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys

sys.path.append(
    os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "imlfapi")
)
from db import GraphsTB, EntityTB


def main():
    # 获取所有图
    graph_info_lst = GraphsTB.find()
    # 循环所有图
    for num, graph_info in enumerate(graph_info_lst):
        # 找到匹配的entity_id
        print("%s / %s" % (num, len(graph_info_lst)))
        if graph_info["entity_id"]:
            continue
        entity_info = EntityTB.find(
            username=graph_info["username"],
            biz_id=graph_info["biz_id"],
            biz_type=graph_info["biz_type"],
        )
        if not entity_info:
            print("没有发现对应的实体graph_info:%s" % graph_info)
            continue
        if len(entity_info) > 1:
            print("发现大于1个实体id: graph_info:%s" % graph_info)
            continue
        # 赋值
        GraphsTB.set(entity_id=entity_info[0]["id"]).where(id=graph_info["id"])
        print("success:%s" % graph_info["id"])


if __name__ == "__main__":
    main()
